<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * [调用js返回]
 * @param string $message
 * @return string
 */
function renderJs($message,$url=''){
    if($url){
        return "<script>alert('$message');window.location.href='$url'</script>";
    }
    return "<script>alert('$message');window.location.href=window.location.href</script>";
}

/**
 * [SwiftMailer]
 * @param $to
 * @param $subject
 * @param $content
 * @return int
 */
function send_email($to,$subject,$content)
{
    $transport=(new Swift_SmtpTransport(config('swift.SWIFT_HOST'),config('swift.SWIFT_SMTP_PORT'),config('swift.SWIFT_SMTP_ENCRYPTION')))
        ->setUsername(config('swift.SWIFT_USERNAME'))
        ->setPassword(config('swift.SWIFT_PASSWORD'));

    $mailer=(new Swift_Mailer($transport));
    $message=(new Swift_Message())
        ->setSubject($subject)
        ->setFrom(array(config('swift.SWIFT_USERNAME')=>'TpBlog Master'))
        ->setTo($to)
        ->setBody($content,'text/html','utf-8');

    return $mailer->send($message);
}

function str_limit($value, $limit = 100, $end = '...')
{
    if (mb_strwidth($value, 'UTF-8') <= $limit) {
        return $value;
    }

    return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
}